package com.ruoyi.resume.provider;

import com.ruoyi.resume.domain.model.resumeProfile.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

public class ResumeSqlProvider {

    // 插入简历主表
    public String insertResume(BasicInfo basicInfo) {
        return new SQL()
                .INSERT_INTO("resume")
                .INTO_COLUMNS(
                        "id","user_id","name", "gender",
                        "phone", "email", "school", "nation",
                        "degree", "residence", "political_status", "age", "skills"
                )
                .INTO_VALUES(
                        "#{id}","0","#{name}", "#{gender}",
                        "#{phone}", "#{email}", "#{school}", "#{nation}",
                        "#{degree}", "#{residence}", "#{politicalStatus}", "#{age}", "#{skills}"
                )
                .toString();
    }

    // 批量插入教育经历
    public String insertEducations(Map<String, Object> params) {
        Integer resumeId = (Integer) params.get("resumeId");
        List<EducationBackground> educations = (List<EducationBackground>) params.get("educations");

        if (CollectionUtils.isEmpty(educations)) {
            return "SELECT 1"; // 空集合不生成 SQL
        }

        // 使用 for 循环生成 SQL
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO education (resume_id, school_name, major_name, start_date, end_date, degree, related_courses) VALUES ");

        for (int i = 0; i < educations.size(); i++) {
            if (i > 0) {
                sql.append(", ");
            }
            sql.append("(")
                    .append(resumeId).append(", ")
                    .append("'").append(educations.get(i).getSchoolName()).append("', ")
                    .append("'").append(educations.get(i).getMajorName()).append("', ")
                    .append("'").append(educations.get(i).getStartTime()).append("', ")
                    .append("'").append(educations.get(i).getEndTime()).append("', ")
                    .append("'").append(educations.get(i).getDegree()).append("', ")
                    .append("'").append(educations.get(i).getRelatedCourses()).append("')");
        }

        return sql.toString();
    }

    // 批量插入实习经历
    public String insertInternships(Map<String, Object> params) {
        Integer resumeId = (Integer) params.get("resumeId");
        List<InternshipExperience> internships = (List<InternshipExperience>) params.get("internships");

        if (CollectionUtils.isEmpty(internships)) {
            return "SELECT 1"; // 空集合不生成 SQL
        }

        // 使用 for 循环生成 SQL
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO internship (resume_id, company_name, job_title, start_date, end_date, project_name, project_description,department) VALUES ");

        for (int i = 0; i < internships.size(); i++) {
            if (i > 0) {
                sql.append(", ");
            }
            sql.append("(")
                    .append(resumeId).append(", ")
                    .append("'").append(internships.get(i).getCompanyName()).append("', ")
                    .append("'").append(internships.get(i).getJobTitle()).append("', ")
                    .append("'").append(internships.get(i).getStartTime()).append("', ")
                    .append("'").append(internships.get(i).getEndTime()).append("', ")
                    .append("'").append(internships.get(i).getProjectName()).append("', ")
                    .append("'").append(internships.get(i).getProjectDescription()).append("', ")
                    .append("'").append(internships.get(i).getDepartment()).append("')");
        }

        return sql.toString();
    }

    // 批量插入工作经历
    public String insertWorks(Map<String, Object> params) {
        Integer resumeId = (Integer) params.get("resumeId");
        List<WorkExperience> works = (List<WorkExperience>) params.get("works");

        if (CollectionUtils.isEmpty(works)) {
            return "SELECT 1"; // 空集合不生成 SQL
        }

        // 使用 for 循环生成 SQL
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO work_experience (resume_id, company_name, job_title, start_date, end_date, project_name, project_description,department) VALUES ");

        for (int i = 0; i < works.size(); i++) {
            if (i > 0) {
                sql.append(", ");
            }
            sql.append("(")
                    .append(resumeId).append(", ")
                    .append("'").append(works.get(i).getCompanyName()).append("', ")
                    .append("'").append(works.get(i).getJobTitle()).append("', ")
                    .append("'").append(works.get(i).getStartTime()).append("', ")
                    .append("'").append(works.get(i).getEndTime()).append("', ")
                    .append("'").append(works.get(i).getProjectName()).append("', ")
                    .append("'").append(works.get(i).getProjectDescription()).append("', ")
                    .append("'").append(works.get(i).getDepartment()).append("')");
        }

        return sql.toString();
    }

    // 批量插入项目经历
    public String insertProjects(Map<String, Object> params) {
        Integer resumeId = (Integer) params.get("resumeId");
        List<ProjectExperience> projects = (List<ProjectExperience>) params.get("projects");

        if (CollectionUtils.isEmpty(projects)) {
            return "SELECT 1"; // 空集合不生成 SQL
        }

        // 使用 StringBuilder 构造批量插入 SQL
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO project_experience (resume_id, project_name, project_description, role, start_date, end_date) VALUES ");

        for (int i = 0; i < projects.size(); i++) {
            if (i > 0) {
                sql.append(", ");
            }
            sql.append("(")
                    .append(resumeId).append(", ") // resume_id
                    .append("'").append(projects.get(i).getProjectName()).append("', ") // project_name
                    .append("'").append(projects.get(i).getDescription()).append("', ") // project_description
                    .append("'").append(projects.get(i).getRole()).append("', ") // role
                    .append("'").append(projects.get(i).getStartDate()).append("', ") // start_date
                    .append("'").append(projects.get(i).getEndDate()).append("')"); // end_date
        }

        return sql.toString();
    }

}